package com.epam.task.web.service.command;

import com.epam.task.web.db.DataBaseReader;
import com.epam.task.web.entity.Order;
import com.epam.task.web.entity.Orders;
import com.epam.task.web.entity.User;
import com.epam.task.web.entity.Users;
import com.epam.task.web.service.AbstractAction;
import com.epam.task.web.util.logging.LoggerUtil;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ShowOrders extends AbstractAction {

    private final LoggerUtil logger = new LoggerUtil(Logger.getLogger(ShowOrders.class.getClass()));

    @Override
    public String perform(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        ResultSet ordersResultSet = null;
        ResultSet usersResultSet = null;
        Orders orders = null;
        User user = null;
        Users users = new Users();


        if (!request.getParameter("user_id").equals("all")) {
            usersResultSet = find("oc_user", "user_id", Integer.valueOf(request.getParameter("user_id")));

            try {
                if (usersResultSet.first()) {
                    long id = (long) usersResultSet.getObject("user_id");
                    user = createUserById(id);
                }
            } catch (SQLException exception) {
                logger.getExceptionTextFileLogger().error(exception);
            }

            ordersResultSet = searchOrders(user.getId());
            orders = new Orders(createOrdersList(ordersResultSet));

            user.setOrders(orders);
            users.getUsers().add(user);
            session.setAttribute("oc_user", users);
        } else {
            usersResultSet = find("oc_user", "user_id", String.valueOf(User.CUSTOMER));

            try {
                while (usersResultSet.next()) {
                    long id = (long) usersResultSet.getObject("user_id");
                    user = createUserById(id);

                    if (user != null) {
                        ordersResultSet = searchOrders(user.getId());
                        orders = new Orders(createOrdersList(ordersResultSet));
                        user.setOrders(orders);
                        users.getUsers().add(user);
                    }
                }
                session.setAttribute("oc_user", users);
            } catch (SQLException exception) {
                logger.getExceptionTextFileLogger().error(exception);
            }
        }

        return request.getParameter("backJsp").toString();
    }

    private List<Order> createOrdersList(ResultSet resultSet) {
        List<Order> jobs = new ArrayList<>();

        try {
            if (resultSet != null) {
                while (resultSet.next()) {
                    int id = (int) resultSet.getObject("order_id");
                    jobs.add(createOrderById(id));
                }
            }

            return jobs;
        } catch (SQLException exception) {
            logger.getExceptionTextFileLogger().error("ShowOrdersAction.createOrdersList(); Line: 126. " + exception);
            return null;
        }
    }

    public ResultSet searchOrders(long customerId) {
        DataBaseReader dataBaseReader = new DataBaseReader();
        ResultSet resultSet = null;
        String query = null;

        try {
            query = "SELECT jobs.* FROM jobs WHERE oc_order.user_id='" + customerId + "';";
            resultSet = dataBaseReader.select(query);
            if (resultSet != null) {
                if (resultSet.next()) {
                    resultSet.beforeFirst();
                    return resultSet;
                }
            } else {
                return null;
            }
        } catch (SQLException exception) {
            logger.getExceptionTextFileLogger().error("ShowOrdersAction.searchOrders(); Line: 155. " + exception);
        }

        return null;
    }

}